﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "page_binom.h"
#include "page_clean.h"
#include "page_puass.h"
#include "page_gg.h"
#include "page_g.h"
#include "page_o.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect( ui->sectionsListWidget, SIGNAL( currentRowChanged(int) ),this, SLOT( sections_changed(int) ) );

    setupPages();
    setupMenu();
    ui->sectionsListWidget->setCurrentRow(0);

    /*splitter fixing*/
    ui->splitter->setStretchFactor( 0, 0 );
    ui->splitter->setStretchFactor( 1, 1 );
    ui->splitter->setSizes(QList<int>() << 200);
}

MainWindow::~MainWindow()
{
    delete ui;
}

 void MainWindow::sections_changed(int index)
 {
     rightBox->changePage(index);
     ui->groupBox_right->setTitle( rightBox->currentPageName() );
 }


 void MainWindow::on_cleanButton_clicked()
 {
     clearLog();
 }
 void MainWindow::on_saveButton_clicked()
 {
	QString selectFile=QFileDialog::getSaveFileName (this,tr("Сохранить файл"),QString(),tr("HTML (*.html)") );
	if(selectFile.isNull()) return;
	QFile file(selectFile);
	if ( !file.open(QIODevice::WriteOnly) )	return;
	QTextStream out(&file);
	out.setCodec("UTF-8");
	out << ui->log->toHtml();
	file.close();
 }
 void MainWindow::appendToLog(const QString &text)
 {
     ui->log->append(text+QString("<br />"));
     QScrollBar * textScroll = ui->log->verticalScrollBar();
     textScroll->setValue(textScroll->maximum());
 }

 void MainWindow::clearLog()
 {
     ui->log->clear();
 }

 void MainWindow::test123(int id)
 {
     id = 0;
 }

 void MainWindow::setupPages()
 {
     pages << new page_binom(this) << new page_puass(this) << new page_gg(this) << new page_g(this) << new page_o(this);

     rightBox = new scrolledStackedWidget(pages,this);
     QVBoxLayout *vbox = new QVBoxLayout();
     vbox->setMargin(1);
     vbox->addWidget(rightBox);
     ui->groupBox_right->setLayout(vbox);
 }

 void MainWindow::setupMenu()
 {
     for( int i=0; i < pages.count(); ++i)
     {
         ui->sectionsListWidget->addItem( pages[i]->accessibleName() );
     }
 }
